<template>
	<view class="user-wap-content">
		<fui-tabs :tabs="tabs" @change="changeTabs" center isFixed :short="false"
			:current="currentTabsIndex"></fui-tabs>

		<view class="tab-body">
			<baseInfo v-show="currentTabsIndex==0" ref="baseInfoRef"></baseInfo>
			<other-info v-show="currentTabsIndex==1" ref="otherInfoRef"></other-info>
			<upload-pic v-show="currentTabsIndex==2" ref="picInfoRef"></upload-pic>
		</view>

		<view class="fix-submit" @click="submitFun">
			<view class="marry-tijiaos">提交</view>
		</view>
	</view>
</template>


<script setup lang="ts">
	import fuiTabs from "@/components/fui-tabs/fui-tabs.vue"
	import baseInfo from "./component/baseInfo.vue"
	import otherInfo from "./component/otherInfo.vue"
	import uploadPic from './component/uploadPic.vue'

	import { onMounted, ref } from 'vue';
	const userObj = uniCloud.importObject("userObj")
	const baseInfoRef = ref()
	const otherInfoRef = ref()
	const picInfoRef = ref()
	const usrinfo = ref({})


	const currentTabsIndex = ref(0)
	const pattern = ref({
		color: '#7A7E83',
		backgroundColor: '#fff',
		selectedColor: '#007AFF',
		buttonColor: '#007AFF',
		iconColor: '#fff'
	})
	const tabs = ref([
		{
			name: '基本信息',
		},
		{
			name: '征婚条件',
		},
		{
			name: '照片墙',
		}


	])
	onMounted(() => {
		getUserInfo()
	})
	const getUserInfo = async () => {
		let res = await userObj.getUserInfo(uniCloud.getCurrentUserInfo().uid, 'onlyUser')
		if (res && res.data && res.data.length > 0) {
			usrinfo.value = res.data[0]
			baseInfoRef.value.baseInfo = res.data[0].baseInfo
			otherInfoRef.value.others = res.data[0].others
			picInfoRef.value.picObj = res.data[0].picObj
			let temp = res.data[0].picObj.pictures.map((item, index) => {
				return ({
					...item,
					isEdit: true
				})
			})
			picInfoRef.value.pictureList.pictures = temp
		}
		console.log(picInfoRef.value.picObj, res)
	}
	const changeTabs = (e) => {
		currentTabsIndex.value = e.index
	}

	const baseFormData = ref({})
	const sexs = ref([{ "value": 0, "text": "女" }, { "value": 1, "text": "男" }])
	const marryHistryItem = ref([{ "value": 0, "text": "未婚" }, { "value": 1, "text": "短婚未育" }, { "value": 2, "text": "离异无孩" }, { "value": 3, "text": "离异有孩" }])
	const submitFun = async () => {
		let baseInfo = null
		let others = null
		try {
			baseInfo = await baseInfoRef.value.commitData()
		} catch {
			return uni.showToast({
				title: '基本信息必填项有误请检查',
				icon: "error"
			})
		}
		try {
			others = await otherInfoRef.value.commitData()
		} catch {
			return uni.showToast({
				title: '征婚信息必填项有误请检查',
				icon: "error"
			})

		}
		if (picInfoRef.value.pictureList.pictures.length === 0) {
			uni.showToast({
				title: '照片墙至少上传一张照片',
				icon: "error"
			})
			return
		}
		let param = {
			userId: uniCloud.getCurrentUserInfo().uid,
			baseInfo: baseInfo.value,
			others: others.value,
			picObj: {
				...picInfoRef.value.picObj,
				pictures: picInfoRef.value.pictureList.pictures
			}
		}

		if (usrinfo.value._id) {
			param = {
				...usrinfo.value,
				...param
			}
			delete param._id
			console.log(param)
			let res = await userObj.updateUser(param)
			if (res && res.data) {
				uni.showToast({
					title: `更新成功,您的编号${res.data.num}`,
					duration: 1500
				});
				setTimeout(() => { uni.navigateBack() }, 1600)
			}
		} else {
			param.branth = true
			let res = await userObj.addUser(param)
			if (res && res.data) {
				uni.showToast({
					title: `提交成功,您的编号${res.data.num}`,
					duration: 1500
				});
				setTimeout(() => { uni.navigateBack() }, 1600)
			}
		}
		console.log(usrinfo.value._id)
	}
</script>

<style lang="scss" scoped>
	.user-wap-content {
		padding-bottom: 104rpx;

		.tab-body {
			margin-top: 88rpx;
		}

		.fix-submit {
			position: fixed;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 110rpx;
			height: 110rpx;
			background-color: #3c3e49;
			border-radius: 90rpx;
			z-index: 11;
			bottom: 60rpx;
			right: 30rpx;
			font-size: 180rpx;
			background-color: rgb(0, 122, 255);

			.marry-tijiaos {
				font-size: 36rpx;
				color: #fff;
			}
		}
	}
</style>